<?php

namespace App\Http\Controllers\Api\Admin;

use App\ActivityBargain;
use App\ActivityGroup;
use App\Goods;
use App\GoodSpu;
use Illuminate\Http\Request;
use Auth;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ActivityController extends Controller
{
    public function setActivityBargain(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $ssn                       = $request->get('ssn');
        $name                      = $request->get('name');
        $mPrice                    = $request->get('m_price');
        $sPrice                    = $request->get('s_price');
        $imgId                     = $request->get('thumb_id');
        $info                      = $request->get('info');
        $integral                  = $request->get('integral');
        $id                        = !is_null($request->get('id'))?$request->get('id'):0;
        $isFreeShipping            = $request->get('isFreeShipping');
        $type                      = $request->get('type');
        $startTime                 = $request->get('start_time');
        $endTime                   = $request->get('end_time');
        $maxMoney                  = $request->get('max_money');
        $maxJoinCount              = $request->get('max_join_count');
        $status                    = $request->get('status');


        /** @var ActivityBargain $newActivityBargain */
        $newActivityBargain = ActivityBargain::whereId($id)->first();
        if(!$newActivityBargain){
            $newActivityBargain = new ActivityBargain();

            $newGoods = new Goods();
        }else{
            /** @var Goods $newGoods */
            $newGoods = Goods::whereId($newActivityBargain->goods_id)->first();
        }

        if(!is_null($ssn))$newGoods->ssn                             = $ssn;
        if(!is_null($name))$newGoods->name                           = $name;
        if(!is_null($mPrice))$newGoods->m_price                      = $mPrice;
        if(!is_null($sPrice))$newGoods->s_price                      = $sPrice;
        if(!is_null($imgId))$newGoods->thumb_id                      = $imgId;
        if(!is_null($info))$newGoods->info                           = $info;
        if(!is_null($integral))$newGoods->integral                   = $integral;
        if(!is_null($isFreeShipping))$newGoods->is_free_shipping     = $isFreeShipping;
        if(!is_null($type))$newGoods->type                           = $type;
        $newGoods->is_shop                                           = 1;
        $newGoods->save();

        $newActivityBargain->goods_id                                   = $newGoods->id;
        if(!is_null($maxMoney))$newActivityBargain->max_money           = $maxMoney;
        if(!is_null($maxJoinCount))$newActivityBargain->max_join_count  = $maxJoinCount;
        if(!is_null($startTime))$newActivityBargain->start_time         = $startTime;
        if(!is_null($endTime))$newActivityBargain->end_time             = $endTime;
        if(!is_null($status))$newActivityBargain->status                = $status;
        $newActivityBargain->save();


        $newGoodsSpu=GoodSpu::whereGoodsId($newGoods->id)->get();

        if(count($newGoodsSpu)<1){
            $newGoodsSpu = new GoodSpu();
            $newGoodsSpu->goods_id      = $newGoods->id;
            $newGoodsSpu->spu_name      = '标准';
            $newGoodsSpu->price         = $newGoods->s_price;
            $newGoodsSpu->status        = 1;
            $newGoodsSpu->save();
        }

        return response()->json(['level' => 'success','message' => '添加或修改成功',"data"=>$newGoods]);
    }

    public function delActivityBargain(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var ActivityBargain $thatActivityBargain */
        $thatActivityBargain =ActivityBargain::whereId($id)->first();
        if(!$thatActivityBargain)
            return response()->json(["level"=>"error","message"=>"该活动不存在！"]);

        $thatGoods = Goods::whereId($thatActivityBargain->goods_id)->first();
        if(!$thatGoods)
            return response()->json(['level' => 'error','message' => '该砍价商品不存在']);

        $thatGoods->delete();

        $thatActivityBargain->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoods]);
    }

    public function setActivityGroup(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $ssn                       = $request->get('ssn');
        $name                      = $request->get('name');
        $mPrice                    = $request->get('m_price');
        $sPrice                    = $request->get('s_price');
        $imgId                     = $request->get('thumb_id');
        $info                      = $request->get('info');
        $integral                  = $request->get('integral');
        $id                        = !is_null($request->get('id')) ? $request->get('id') : 0;
        $isFreeShipping            = $request->get('isFreeShipping');
        $type                      = $request->get('type');
        $startTime                 = $request->get('start_time');
        $endTime                   = $request->get('end_time');
        $maxGroupNum               = $request->get('max_group_num');
        $groupEffectiveTime        = $request->get('group_effective_time');
        $status                    = $request->get('status');


        /** @var ActivityGroup $newActivityGroup */
        $newActivityGroup = ActivityGroup::whereId($id)->first();
        if(!$newActivityGroup){
            $newActivityGroup = new ActivityGroup();

            $newGoods = new Goods();
        }else{
            /** @var Goods $newGoods */
            $newGoods = Goods::whereId($newActivityGroup->goods_id)->first();
        }

        if(!is_null($ssn))$newGoods->ssn                             = $ssn;
        if(!is_null($name))$newGoods->name                           = $name;
        if(!is_null($mPrice))$newGoods->m_price                      = $mPrice;
        if(!is_null($sPrice))$newGoods->s_price                      = $sPrice;
        if(!is_null($imgId))$newGoods->thumb_id                      = $imgId;
        if(!is_null($info))$newGoods->info                           = $info;
        if(!is_null($integral))$newGoods->integral                   = $integral;
        if(!is_null($isFreeShipping))$newGoods->is_free_shipping     = $isFreeShipping;
        if(!is_null($type))$newGoods->type                           = $type;
        $newGoods->is_shop                                           = 1;
        $newGoods->save();

        $newActivityGroup->goods_id                                               = $newGoods->id;
        if(!is_null($maxGroupNum))$newActivityGroup->max_group_num                = $maxGroupNum;
        if(!is_null($groupEffectiveTime))$newActivityGroup->group_effective_time  = $groupEffectiveTime;
        if(!is_null($startTime))$newActivityGroup->start_time                     = $startTime;
        if(!is_null($endTime))$newActivityGroup->end_time                         = $endTime;
        if(!is_null($status))$newActivityGroup->status                            = $status;
        $newActivityGroup->save();


        $newGoodsSpu=GoodSpu::whereGoodsId($newGoods->id)->get();

        if(count($newGoodsSpu)<1){
            $newGoodsSpu = new GoodSpu();
            $newGoodsSpu->goods_id      = $newGoods->id;
            $newGoodsSpu->spu_name      = '标准';
            $newGoodsSpu->price         = $newGoods->s_price;
            $newGoodsSpu->status        = 1;
            $newGoodsSpu->save();
        }

        return response()->json(['level' => 'success','message' => '添加或修改成功',"data"=>$newGoods]);
    }

    public function delActivityGroup(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var ActivityGroup $thatActivityGroup */
        $thatActivityGroup =ActivityGroup::whereId($id)->first();
        if(!$thatActivityGroup)
            return response()->json(["level"=>"error","message"=>"该活动不存在！"]);

        $thatGoods = Goods::whereId($thatActivityGroup->goods_id)->first();
        if(!$thatGoods)
            return response()->json(['level' => 'error','message' => '该砍价商品不存在']);

        $thatGoods->delete();

        $thatActivityGroup->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoods]);
    }
}
